<template>
  <page-wrapper>
    <a-card :bordered="false" class="mb-3 proCard">
      <a-space align="center">
        <a-input-group style="width: 380px">
          <a-input
            :style="{ width: '100%' }"
            v-model="params.roleName"
            clearable
            placeholder="请输入区域名称"
            @keyup.enter="reloadTable"
          />
          <a-button type="primary" @click="reloadTable">
            <template #icon>
              <a-icon>
                <SearchOutlined />
              </a-icon>
            </template>
            查询
          </a-button>
        </a-input-group>
      </a-space>
    </a-card>
    <a-card :bordered="false" class="proCard">
      <BasicTable
        :columns="columns"
        :request="loadDataTable"
        row-key="id"
        ref="tableRef"
        :cascade="false"
        :pagination="false"
        allow-checking-not-loaded
        :actionColumn="actionColumn"
        :load-more="onLoad"
        @update:checked-row-keys="onCheckedRow"
      >
        <template #tableTitle>
          <a-button type="primary" @click="openCreateDrawer">
            <template #icon>
              <a-icon>
                <FileAddOutlined />
              </a-icon>
            </template>
            添加区域
          </a-button>
        </template>

        <template #action>
          <TableAction />
        </template>
      </BasicTable>
    </a-card>

    <CreateDrawer
      ref="createDrawerRef"
      :title="drawerTitle"
      :regionList="regionTreeData"
      @change="reloadTable"
    />
  </page-wrapper>
</template>

<script lang="ts" setup>
  import { reactive, ref, unref, h, onMounted } from 'vue';
  import { Message } from '@arco-design/web-vue';
  import { BasicTable, TableAction, BasicColumn } from '@/components/Table';
  import { FileAddOutlined, SearchOutlined } from '@vicons/antd';
  import { columns } from './columns';
  import { provinceList, parentList, regionTreeList, deleteRegion } from '@/api/system/region';
  import CreateDrawer from './CreateDrawer.vue';

  const tableRef = ref();
  const createDrawerRef = ref();
  const drawerTitle = ref('添加区域');
  const regionTreeData = ref([]);

  const params = reactive({
    roleName: '',
  });

  const actionColumn: BasicColumn = reactive({
    width: 175,
    title: '操作',
    key: 'action',
    fixed: 'right',
    render({ record }) {
      return h(TableAction as any, {
        style: 'button',
        actions: [
          {
            label: '删除',
            popConfirm: {
              content: '您确定要删除吗？',
              okText: '确定',
              cancelText: '取消',
              onOk: handleDelete.bind(null, record),
            },
          },
          {
            label: '编辑',
            onClick: handleEdit.bind(null, record),
          },
        ],
      });
    },
  });

  const loadDataTable = async (res: any) => {
    let _params = {
      ...unref(params),
      ...res,
    };
    const tableData = await provinceList(_params);
    tableData.forEach((item) => {
      item.isLeaf = false;
    });
    return {
      list: tableData,
    };
  };

  async function onLoad(record, done) {
    const list = await parentList({ parentId: record.id });
    list.forEach((item) => {
      item.isLeaf = item.isDistrict;
    });
    // record.children = list;
    done(list);
  }

  function openCreateDrawer() {
    drawerTitle.value = '添加区域';
    const { openDrawer } = createDrawerRef.value;
    openDrawer();
  }

  function onCheckedRow(rowKeys: any[]) {
    console.log(rowKeys);
  }

  function reloadTable() {
    tableRef.value.reload();
  }

  function handleEdit(record: Recordable) {
    console.log('点击了编辑', record);
    drawerTitle.value = '编辑区域';
    const { openDrawer } = createDrawerRef.value;
    openDrawer(record.id);
  }

  function handleDelete(record: Recordable) {
    deleteRegion({ id: record.id }).then(() => {
      Message.success('删除成功');
      reloadTable();
    });
  }

  onMounted(async () => {
    const list = await regionTreeList();
    regionTreeData.value = list;
  });
</script>

<style lang="less" scoped></style>
